package net.esj.basic.core.view.tools.searcher;

import java.util.Map;
import java.util.Map.Entry;

import net.esj.basic.core.view.action.SearcherActionAware;
import net.esj.basic.core.view.tools.ActionContext;
import net.esj.basic.dao.querybuilder.OrderExpression;
import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.dao.querybuilder.SymbolExpression;
import net.esj.basic.utils.Pagination;
import net.esj.basic.utils.hibe.HiCriteria;
import net.esj.basic.utils.view.JavaTypeFormater;

public abstract class SearcherUtils {

	public static void appendQueryBuilder(Searcher searcher,QueryBuilder qb){
		for(SearcherColumn sc : searcher.getColumns()){
			SymbolExpression.append(qb, sc.getKey(),sc.getType() , sc.getValue());
		}
		
		for(SearcherColumn or : searcher.getOrders()){
			OrderExpression.append(qb, or.getValue().toString(), or.getKey());
		}
	}
	
	public static void pageQueryBuilder(Pagination pagi){
		if(pagi.getQueryBuilder()==null){
			return;
		}
		if(pagi.getSearcher()!=null){
			for(SearcherColumn sc : pagi.getSearcher().getColumns()){
				SymbolExpression.append(pagi.getQueryBuilder(), sc.getType(), sc.getKey(), sc.getValue());
			}
			
			for(SearcherColumn or : pagi.getSearcher().getOrders()){
				OrderExpression.append(pagi.getQueryBuilder(), or.getValue().toString(), or.getKey());
			}
		}
		if(pagi.getQuery()!=null && !pagi.getQuery().isEmpty()){
			for(Object o: pagi.getQuery().entrySet()){
				Map.Entry<String, String> entry = (Entry<String, String>) o;
				String[] tmps =parseOperatorAndQueryname(entry.getKey());
				if("order".equals(tmps[1])){
					OrderExpression.append(pagi.getQueryBuilder(), entry.getValue(), tmps[0]);
				}else{
					SymbolExpression.append(pagi.getQueryBuilder(), tmps[1], tmps[0],  parseValue(entry.getValue(), entry.getKey()));
				}
			}
		}
		
	}
	
	public static void resolveQueryParams(Searcher searcher,Map<String,String> queryParams){
		if(queryParams!=null && !queryParams.isEmpty()){
			for(Object o: queryParams.entrySet()){
				Map.Entry<String, String> entry = (Entry<String, String>) o;
				Object v = parseValue(entry.getValue(), entry.getKey());
				String[] opAndqn = parseOperatorAndQueryname(entry.getKey());
				searcher.newColumn(opAndqn[0], opAndqn[0], opAndqn[1], v);
			}
		}
	}
	
	public static void queryCriteria(HiCriteria criteria,String key,String value){
		String[] tmps =parseOperatorAndQueryname(key);
		SymbolExpression.append(criteria, SymbolExpression.getValueWithException(tmps[1]), tmps[0],  parseValue(value, key));
	}
	
	public static Object parseValue(String value,String key){
		if(key.indexOf("?")>0){
			String k = key.substring(key.indexOf("?")+1);
			return JavaTypeFormater.formate(k, value);
		}else{
			return value;
		}
	}
	
	public static String[] parseOperatorAndQueryname(String name){
		String[] value = new String[2];
		if(name.indexOf("?")>0){
			name = name.substring(0, name.indexOf("?"));
		}
		if(name.indexOf("_")>0){
			value[0] = name.substring(0,name.indexOf("_"));//key
			value[1]  = name.substring(name.indexOf("_")+1);//type
		}else{
			value[0] = name;//key
			value[1]  = SymbolExpression.EQUAL.getKey();//type
		}
		return value;
	}

}
